package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Login;
import oa.entity.User;
import oa.service.LoginService;
import oa.service.UserService;
import oa.service.impl.LoginServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class Loginservlet
 */
@WebServlet("/loginservlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session  = request.getSession();
		UserService userservice = new UserServiceImpl();
		LoginService loginService = new LoginServiceImpl();
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String opt = request.getParameter("opt");
		request.getSession().setAttribute("names", name);
		String loginopt =null;
		if ("user".equals(opt)) {
			User user = userservice.userLogin(name, pwd);   
			
			if (user == null) {
				response.sendRedirect("user_login.jsp");
			} else {
				session.setAttribute("loginopt", "user");
			    session.setAttribute("usercompanyid", user.getCompanyId());
			    session.setAttribute("userid", user.getUserId());
			    session.setAttribute("departid", user.getDeptId());
			    session.setAttribute("competence", user.getUserCompetence());
				session.setAttribute("UserLoginName", name);
				response.sendRedirect("indexServlet");
			}
		} else {
			Login login = loginService.loginCheck(name, pwd);
			
			if (login == null) {
				response.sendRedirect("user_login.jsp");
			} else {
				session.setAttribute("loginopt", "admin");
				session.setAttribute("logincompanyid", login.getCompanyId());
			    session.setAttribute("loginuserid", login.getLoginId());
			    session.setAttribute("competence", 3);
			    session.setAttribute("LoginName", name);
				response.sendRedirect("indexServlet");
			   
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
